;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var router = require("../hash/pageRouter");
	var validate = require("validate");
	var databind = require("databind");
	var dataSourceAdd = require("./dataSourceAdd.html");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var oData = {};
	var dialogCss = require("css_dialog");
	var tools = require("tools");
	var init = function() {
		router.init();
		oData.pageIndex = pageIndex;
		oData.pageSize = pageSize;
		inquireDataSourcePageList();
		initEvent();
	};

	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		};
		
		$("#dataSource .createDataSource").unbind("click").click(function(){
			$("#popdialog").html(dataSourceAdd);
			dialog.showDialog($("#add_dataSource_dialog"));
			$("#add_dataSource_dialog .sure_yes").unbind("click").click(function(){
				databind.submitDom($("#add_dataSource_dialog"), function(){
					base.alert(1, "新增成功！");
					dialog.hideDialog($("#add_dataSource_dialog"));
					router.route("datasource/dataSource.html");
				});
			});
			
			$("#add_dataSource_dialog .sure_no").unbind("click").click(function(){
				dialog.hideDialog($("#add_dataSource_dialog"));
			});
		});
	};
	
	//权限控制
	var inquireCurrentOperationList = function() {
		base.fnAjax({
			action: 'inquireCurrentOperationList.json',
			async: false,
			fnSuccess: function(data) {
				if (data.status == 200) {
					for (var i = 0; i < data.datas.length; i++) {
						var permission = data.datas[i];
						var permissionId = permission.permissionId;
						$.each($("#memberGrade .permissionLink"), function() {
							var $this = $(this);
							if ($this.attr("permissionId") == permissionId) {
								$this.removeClass("none");
							}
						});
					}
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};

	//查询列表
	var inquireDataSourcePageList = function() {
		tools.loadList("inquireDataSourcePageList.json", $("#dataSource table"), $("#dataSourceTpl"), oData, function(){
			inquireCurrentOperationList();
			
			$("#dataSource .disabled").unbind("click").click(function(){
				var $this = $(this);
				dialog.showConfirmDialog("停用卡类", "是否确定停用该数据来源？", function(){
					disableDataSource($this.parents("tr").attr("dataSourceId"));
				});
			});
			
			$("#dataSource .enabled").unbind("click").click(function(){
				var $this = $(this);
				dialog.showConfirmDialog("启用来源", "是否确定启用该数据来源？", function(){
					enableDataSource($this.parents("tr").attr("dataSourceId"));
				});
			});
		}, $("#dataSource .pagerCon"));
	};
	
	//启用卡类
	var enableDataSource = function(dataSourceId) {
		base.fnAjax({
			action: 'enableDataSource.json',
			async: false,
			oData: {
				dataSourceId : dataSourceId
			},
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "启用成功！");
					router.route("datasource/dataSource.html");
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	//停用卡类
	var disableDataSource = function(dataSourceId) {
		base.fnAjax({
			action: 'disableDataSource.json',
			async: false,
			oData: {
				dataSourceId : dataSourceId
			},
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "停用成功！");
					router.route("datasource/dataSource.html");
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};

	return {
		init: init
	};
});